﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using Caliburn.Micro.BikeWizard.Data;
using Caliburn.Micro.BikeWizard.Utils;

namespace Caliburn.Micro.BikeWizard.Services
{
    public class InMemoryDataStore : IDataStore
    {
        #region Private Members
        private Dictionary<Type, List<object>> data = new Dictionary<Type, List<object>>();
        #endregion

        #region Public Methods
        public IEnumerable<T> GetAll<T>()
        {
            var objectList = GetOrCreateList<T>();
            var castedObjectList = objectList.Cast<T>();
            return castedObjectList;
        }

        public void Add<T>(T instance)
        {
            var instances = GetOrCreateList<T>();
            if (!instances.Contains(instance))
            {
                instances.Add(instance);
            }
        }

        public void AddRange<T>(IEnumerable<T> instancesToAdd)
        {
            var instances = GetOrCreateList<T>();

            foreach (T instanceToAdd in instancesToAdd)
            {
                if (!instances.Contains(instanceToAdd))
                {
                    instances.Add(instanceToAdd);
                }
            }
        }

        public void ClearData<T>()
        {
            GetOrCreateList<T>().Clear();
        }
        #endregion

        #region Private Methods
        private List<object> GetOrCreateList<T>()
        {
            if (data.ContainsKey(typeof(T)))
            {
                data[typeof(T)].Cast<T>().ToList();
            }
            else
            {
                data.Add(typeof(T), new List<object>());
            }

            return data[typeof(T)];
        }
        #endregion
    }
}
